﻿using System;

using System.Collections.Generic;
using System.IO;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using System.Xml;
using MelectricComponents;

namespace FileExplorer
{
    public class AddMoneyWindow : Window
    {
        private Toolbar toolbar;
        private BackgroundDecorator background;
        TextBox txtDateTime;
        DropDownButton ddlCategory;
        TextBox txtCategory;
        DropDownButton ddlPaymentMethod;
        TextBox txtItem;
        TextBox txtCost;
        
        public AddMoneyWindow()
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);

            txtDateTime = new TextBox
            {
                Alpha = 128
               ,
                ToolTip = new DatePickerToolTip()
               ,
                Size = new Size(Screen.Width, 100)
               ,
                Location = new Point(0, 0)
               ,
                Text = DateTime.Now.ToString("dd/MM/yyyy")
            };
            Controls.Add(txtDateTime);
            txtCategory = new TextBox
                                    {
                                        Visible=false
                                        ,
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 * 4
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                        ,
                                        Location = new Point(0, 100)
                                    };
            Controls.Add(txtCategory);
            ddlCategory = new DropDownButton { Bounds = new Rectangle(0, 100, Screen.Size.Width / 8 * 4, 100), Text = "Category", Color = Color.Snow, TextColor = Color.Black };
            ddlCategory.AddItems("Food", "Snacks", "Gadgets", "Cat", "Fish", "House", "Eating out", "Car", "Other");
            Controls.Add(ddlCategory);
            ddlCategory.ItemClicked += new EventHandler<ItemEventArgs>(OnItemClicked);
            ddlPaymentMethod = new DropDownButton { Bounds = new Rectangle(Screen.Size.Width / 8 * 4, 100, Screen.Size.Width / 8 * 4, 100), Text = "Card", Color = Color.Snow, TextColor = Color.Black };
            ddlPaymentMethod.AddItems("Card", "Cash");
            Controls.Add(ddlPaymentMethod);
            txtItem = new TextBox
            {
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 5
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                ,
                Location = new Point(0, 200)
            };
            Controls.Add(txtItem);
            Label lblItem = new Label
            {
                Text = "Item"
                ,
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 3
                ,
                Location = new Point(Screen.Size.Width / 8 * 5, 200)
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
            };
            Controls.Add(lblItem);
            txtCost = new TextBox
            {
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 5
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                ,
                Location = new Point(0, 300)
            };
            Controls.Add(txtCost);

            Label lblCost = new Label
                                    {
                                        Text = "Cost"
                                        ,
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 *3
                                        ,
                                        Location = new Point(Screen.Size.Width / 8 * 5, 300)
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                    };
            Controls.Add(lblCost);

            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Cancel";
            toolbar.RightButton.Text = "Submit";
            toolbar.LeftButton.Click += (s, e) => { txtCategory.Visible = false; ddlCategory.Visible = true; Hide(TransitionMode.ZoomOut); };
            toolbar.RightButton.Click += (s, e) => { SaveData(); };
        }
        private void SaveData()
        {
            Dictionary<string, string> Attributes = new Dictionary<string, string>();
            Attributes.Add("DateTime", txtDateTime.Text);
            Attributes.Add("Amount", txtCost.Text);
            Attributes.Add("Item", txtCategory.Text);
            Attributes.Add("PaymentMethod", ddlPaymentMethod.Text);
            XMLUtility oXMLUtility = new XMLUtility();
            oXMLUtility.SaveXML("Money", "Transaction", Attributes);

            string sItem = txtCategory.Text;
            string sCost = txtCost.Text;



            ConfirmDialog dlg = new ConfirmDialog("Money Complete", sCost + " spent on " + sItem , true);
            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
            {
                txtCategory.Text = "";
                txtItem.Text = "";
                txtCost.Text = "";
                ddlCategory.Text = "Category";
                ddlPaymentMethod.Text = "Card";
                txtDateTime.Text = DateTime.Now.ToString("dd/MM/yyyy");
                Hide(TransitionMode.ZoomOut);
            });

        }

        protected virtual void OnItemClicked(object sender, ItemEventArgs e)
        {
            // if (ItemClicked != null) dropDown.ItemClicked(this, e);
            MenuItem item = e.Item as MenuItem;
            if (item != null)
            {
                if (item.Text == "Other")
                {
                    ddlCategory.Visible = false;
                    txtCategory.Text = "";
                    txtCategory.Visible = true;

                }
                else
                {
                    txtCategory.Text = item.Text;
                }


            }

        }
    }
}
